package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.service.BusAppointmentService;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.UpdateUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static cn.wolfcode.business.domain.BusAppointment.*;

@Service
public class BusAppointmentServiceImpl implements BusAppointmentService {
    @Autowired
    BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private BusAppointmentService busAppointmentService;

    /**
     * 查询所有的养修预约的信息
     * @param busAppointment
     * @return
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment) {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }
    /**
     * 根据id查询一条养修预约的信息
     *
     * @param id
     * @return
     */
    @Override
    public BusAppointment getInfo(Long id) {
        return busAppointmentMapper.getInfo(id);
    }
    /**
     * 新增一条养修预约的信息
     *
     * @param busAppointment
     * @return
     */
    @Override
    public Integer add(BusAppointment busAppointment) {
        //1.首先进行参数合理化验证
        //1.1 6个参数的非空验证
        if (busAppointment.getCustomerName() == null || busAppointment.getCustomerPhone() == null
                || busAppointment.getAppointmentTime() == null || busAppointment.getLicensePlate() == null
                || busAppointment.getCarSeries() == null || busAppointment.getServiceType() == null)
            throw new RuntimeException("必选项不能为空！！！！");
        //1.2 对联系方式即电话进行格式校验
        if (RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone()) != true)
            throw new RuntimeException("手机号码格式不正确，请重新输入！！！");
        //1.3 对车牌号进行格式校验
        if (VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate()) == null)
            throw new RuntimeException("车牌号格式不正确，请重新输入！！！");
        //1.4 对预约时间进行合理化验证即至少大于1分钟
        Date date = UpdateUtils.addTime(new Date(), Calendar.MINUTE, 1);
        //当前系统时间（+amount后）在预约时间之前
        //手写一个工具类，传入一个系统当前时间，一个时间类型，一个要增加的时间
        if (!date.before(busAppointment.getAppointmentTime()))
            throw new RuntimeException("预约时间超前，请您选择后续时间");
        //确保安全创建一个中间对象
        BusAppointment newBusAppointment = new BusAppointment();
        newBusAppointment.setCustomerName(busAppointment.getCustomerName());
        newBusAppointment.setCustomerPhone(busAppointment.getCustomerPhone());
        newBusAppointment.setAppointmentTime(busAppointment.getAppointmentTime());
        newBusAppointment.setCarSeries(busAppointment.getCarSeries());
        newBusAppointment.setLicensePlate(busAppointment.getLicensePlate());
        newBusAppointment.setServiceType(busAppointment.getServiceType());
        newBusAppointment.setInfo(busAppointment.getInfo());
        //确保安全手动设置状态值为 预约中
        newBusAppointment.setStatus(STATUS_BOOKING);
        //创建时间
        newBusAppointment.setCreateTime(new Date());
        return busAppointmentMapper.add(newBusAppointment);
    }

    /**
     * 新增和修改的区别在哪？什么时候能够修改？
     * 预约单只有状态是预约中的时候可以修改
     * @param busAppointment
     * @return
     */
    @Override
    public Integer update(BusAppointment busAppointment) {
        //1.首先进行参数合理化验证
        //1.1 6个参数的非空验证
        if (busAppointment.getCustomerName() == null || busAppointment.getCustomerPhone() == null
                || busAppointment.getAppointmentTime() == null || busAppointment.getLicensePlate() == null
                || busAppointment.getCarSeries() == null || busAppointment.getServiceType() == null)
            throw new RuntimeException("必选项不能为空！！！！");
        //只有预约中的状态才能被修改
        if (!getInfo(busAppointment.getId()).getStatus().equals(busAppointment.getStatus())) {
            throw  new RuntimeException("非法修改状态，哥们你小心了！！！");
        }
        if (!STATUS_BOOKING.equals(busAppointment.getStatus())) {
            throw  new RuntimeException("只有预约中的才能进行编辑！！！");
        }

        //1.2 对联系方式即电话进行格式校验
        if (RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone()) != true)
            throw new RuntimeException("手机号码格式不正确，请重新输入！！！");
        //1.3 对车牌号进行格式校验
        if (VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate()) == null)
            throw new RuntimeException("车牌号格式不正确，请重新输入！！！");
        //1.4 对预约时间进行合理化验证即至少大于1分钟
        Date date = UpdateUtils.addTime(new Date(), Calendar.MINUTE, 1);
        //当前系统时间（+amount后）在预约时间之前
        //手写一个工具类，传入一个系统当前时间，一个时间类型，一个要增加的时间
        if (!date.before(busAppointment.getAppointmentTime()))
            throw new RuntimeException("预约时间超前，请您选择后续时间");
        return busAppointmentMapper.update(busAppointment);
    }

    /**
     * 到店功能的实现：到店功能就是修改添加一个到店时间和将状态修改为已到店
     * @param id
     * @return
     */
    @Override
    public Integer arrival(Long id) {
        if (id==null){
            throw new RuntimeException("参数不合理！！！");
        }
        if (!STATUS_BOOKING.equals(getInfo(id).getStatus())) {
            throw new RuntimeException("只有预约中才能进行到店！！！");
        }
        //需要传递三个参数  到店时间，状态，id
        return busAppointmentMapper.arrival(new Date(),STATUS_ARRIVED,id);
    }

    @Override
    public Integer cancel(Long id) {

        if (id==null){
            throw new RuntimeException("参数不合理！！！");
        }
        if (!STATUS_BOOKING.equals(getInfo(id).getStatus())) {
            throw new RuntimeException("只有预约中才能取消！！！");
        }
        //需要传递三个参数  到店时间，状态，id
        return busAppointmentMapper.cancel(STATUS_USER_CANCELLED,id);
    }

    @Override
    public Integer deleteById(Long id) {
        //参数合理化验证
        if (id==null){
            throw new RuntimeException("参数不合理！！！");
        }
        if (!STATUS_BOOKING.equals(getInfo(id).getStatus())) {
            throw new RuntimeException("只有预约中才能删除！！！");
        }
        return busAppointmentMapper.deleteById(id);
    }

    @Override
    public int payUpdateStates(Long appointId) {
        return busAppointmentMapper.payUpdateStates(appointId,STATUS_PAID);
    }


}
